package Classes.GameViews.ViewDisplayers// ActionScript file
{
	import Classes.GameDatas.Deal_LianJiReward;
	import Classes.GameDatas.GD_Item;
	import Classes.GameDatas.GD_Player;
	import Classes.GameDatas.GD_Reward;
	import Classes.GameDatas.GD_Weapon;
	import Classes.GameErrors.GameError;
	import Classes.GameUtils.GD_deal;
	import Classes.GameUtils.GU_Sound;
	import Classes.GameUtils.GU_Vector2D;
	import Classes.GameUtils.GU_dealWeapon;
	import Classes.GameViews.ViewIcons.ViewIcon;
	import Classes.GameViews.ViewPanels.VP_Main;
	import Classes.Managers.M_Timer;
	
	import com.greensock.TweenLite;
	
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.events.TimerEvent;
	import flash.geom.Point;
	import flash.utils.Timer;

	
	/**
	 *抛出奖励显示类
	 * @author 葛增文
	 * 
	 */	
	public class VD_Reward extends ViewDisplayer
	{
		public static const GRAVITY:Number=5;/*模拟重力*/
		public static const JUMPTIME:int=2;/*跳动次数*/
		private var _initVY:Number;/*初始跳跃高度*/
		private var _icon:ViewIcon;/*奖励图标*/
		private var _isDropping:Boolean;/*掉落状态*/
		private var _isGetting:Boolean;/*拾取状态*/
		private var _getPoint:Point;/*拾取移动目标*/
		private var _limit_y:Number;/*初始Y坐标*/
		private var _jumpNum:int=0;/*已跳动次数*/
		private var _velocity:GU_Vector2D;/*速率，2D向量*/
		
		/**
		 * 掉落物品的原始坐标  暂时测试 
		 */
		private static var pointObj:Object;
		
		public function VD_Reward(gd:GD_Reward):void
		{
			super(gd);
			this.init();
		}
		
		private function init():void
		{
			this.mouseEnabled=false;
			this.addListener();
			this.beginDrawMyView();
		}
		
		private function addListener():void
		{
			this.addEventListener(MouseEvent.ROLL_OVER,overHandler);
			
			var time:Timer	= new Timer(10000,1);
			time.addEventListener(TimerEvent.TIMER, autoBeGetted);
			time.start();
		}
		
		private function removeListener():void
		{
			this.removeEventListener(MouseEvent.ROLL_OVER,overHandler);
		}
		
		override protected function beginDrawMyView():void
		{
			this.draw();
		}
		
		private function draw():void
		{
			if(this.icon)
			{
				this.removeChild(this.icon);
			}
			switch(this.rewardData.type)
			{
				case 0://物品
					var sysItemType:String=GD_deal.instance.reutrnType(this.rewardData.par);
					if(sysItemType=="1")
					{
						var weaponData:GD_Weapon=GD_Weapon.creat({itemType:this.rewardData.par});
						this._icon=weaponData.creatTmpIcon();
						pointObj={x:0,y:0};
					}else{
						var itemData:GD_Item=GD_Item.creat({itemType:this.rewardData.par})
						this._icon=itemData.creatTmpIcon();
						pointObj={x:0,y:0};
					}
					break;
				case 1://行动力
					this._icon=ViewIcon.creat({sourceName:"icon0.swf",picName:"Icon_Act",creatNum:this.rewardData.par});
					pointObj={x:88,y:45};
					break;
				case 2://金币
					this._icon=ViewIcon.creat({sourceName:"icon0.swf",picName:"Icon_JB",creatNum:this.rewardData.par});
					pointObj={x:392,y:9};
					break;
				case 3://O币
					this._icon=ViewIcon.creat({sourceName:"icon0.swf",picName:"Icon_OB",creatNum:this.rewardData.par});
					pointObj={x:600,y:9};//{x:567,y:9};
					break;
				case 4://经验
					this._icon=ViewIcon.creat({sourceName:"icon0.swf",picName:"Icon_Exp",creatNum:this.rewardData.par});
					pointObj={x:90,y:16};
					break;
				case 5://权限
					break;
				default:
					try{
						var sysItemType2:String=GD_deal.instance.reutrnType(this.rewardData.type);
						if(sysItemType2=="1")
						{
							var weaponData2:GD_Weapon=GD_Weapon.creat({itemType:this.rewardData.type,itemNum:this.rewardData.par});
							this._icon=weaponData2.creatTmpIcon();
							pointObj={x:0,y:0};
						}else{
							var itemData2:GD_Item=GD_Item.creat({itemType:this.rewardData.type,itemNum:this.rewardData.par})
							this._icon=itemData2.creatTmpIcon();
							pointObj={x:0,y:0};
						}
					}catch(error:GameError){
						throw new GameError("没有此中类型（"+this.rewardData.type+"）的奖励!")
					}
					
					
			}
			if(this.icon)
			{
				this.icon.scaleX=0.5;
				this.icon.scaleY=0.5;
				this.addChild(this.icon);
			}
		}
		
		/**
		 *创建抛出奖励显示对象 
		 * @param creatData 数据对象
		 * @return 显示对象
		 * 
		 */			
		public static function creat(creatData:GD_Reward):VD_Reward
		{
			var creatTarget:VD_Reward=new VD_Reward(creatData);
			if(null!=pointObj)
			{
				creatTarget._getPoint=new Point(pointObj.x,pointObj.y);
			}
			return creatTarget;
		}
		
		/**
		 *设定抛出位置和抛出速度 
		 * @param atx
		 * @param aty
		 * @param vx
		 * @param vy
		 * 
		 */						
		public function setDropping(atx:Number,aty:Number,vx:Number,vy:Number,excursiony:Number):void
		{
			this.x=atx;
			this.y=aty;
			this.initVY=vy;
			this.limit_y=aty+excursiony;
			this.velocity=new GU_Vector2D(vx,vy);
			this.isDropping=true;
		}
		
		public function update():void
		{
			if(this.isDropping)
			{
				var setY:Number=this.y+this.velocity.y;
				if(setY<this.limit_y)
				{
					this.y=setY;
					this.x+=this.velocity.x;
					this.velocity.y+=GRAVITY;
				}
				else
				{
					var fallDist:Number=this.limit_y-this.y;
					this.y=this.limit_y;
					this.x+=fallDist/Math.tan(this.velocity.angel);
					if(this.jumpNum<3)
					{
						this.jumpNum+=1;
						this.velocity.y=this.initVY/(this.jumpNum*2);
					}
					else
					{
						this.isDropping=false;
					}
				}
			}
		}
		
		private function overHandler(e:MouseEvent):void
		{
			if(!this.isGetting)this.beGetted();
		}
		
	
		
		/**
		 *自动得到奖励(奖励品向目标位置飞行，没有连锁反应); 
		 * 
		 */		
		private function autoBeGetted(e:Event=null):void
		{
			(e.target as Timer).removeEventListener(TimerEvent.TIMER, autoBeGetted);
			if(this.isGetting)return;
			
			if(null == this.parent)return;
			this.isDropping=false;
			this.isGetting=true;
			this.draw();
			var locationPoint:Point=this.parent.globalToLocal(new Point(pointObj.x,pointObj.y));
			TweenLite.to(this,0.5,{x:locationPoint.x,y:locationPoint.y,onComplete:getOver});
//			Deal_LianJiReward.instance.opeaterReward();
			/*
			switch(this.rewardData.type)
			{
				case 0://物品
					GU_Sound.instance.playCommSound("System_Collect_Props");
					break;
				case 1://行动力
					GU_Sound.instance.playCommSound("System_Collect_Power");
					break;
				case 2://金币
					GU_Sound.instance.playCommSound("System_Collect_GB");
					break;
				case 3://O币
					GU_Sound.instance.playCommSound("System_Collect_GB");
					break;
				case 4://经验
					GU_Sound.instance.playCommSound("System_Collect_Exp");
					break;
				case 5://权限
					break;
				default:
					throw new GameError("没有这种奖励");
			}*/
			
		}
		
		
		
		/**
		 *点击得到奖励(奖励品向目标位置飞行); 
		 * 
		 */		
		private function beGetted():void
		{
			this.isDropping=false;
			this.isGetting=true;
			var locationPoint:Point=this.parent.globalToLocal(this.getPoint);
			TweenLite.to(this,0.5,{x:locationPoint.x,y:locationPoint.y,onComplete:getOver});
			Deal_LianJiReward.instance.opeaterReward();
			
			switch(this.rewardData.type)
			{
				case 0://物品
					GU_Sound.instance.playCommSound("System_Collect_Props");
					this.playNewWeaponWin(this.rewardData.par);
					break;
				case 1://行动力
					GU_Sound.instance.playCommSound("System_Collect_Power");
					break;
				case 2://金币
					GU_Sound.instance.playCommSound("System_Collect_GB");
					break;
				case 3://O币
					GU_Sound.instance.playCommSound("System_Collect_GB");
					break;
				case 4://经验
					GU_Sound.instance.playCommSound("System_Collect_Exp");
					break;
				case 5://权限
					break;
				default://默认为掉落物品的声音
					GU_Sound.instance.playCommSound("System_Collect_Props");
					this.playNewWeaponWin(this.rewardData.type);
			}
			
		}
		
		/**
		 * 处理新武器弹出框的问题 
		 */		
		private function playNewWeaponWin(itemId:Number):void
		{
			var citemType:String=GD_deal.instance.reutrnType(itemId);
			if(citemType=="1" && null!=GD_Player.instance.addNewWeapons && null!=GD_Player.instance.addNewWeapons[itemId] )
			{
				GU_dealWeapon.instance.hanldNewWeapon(GD_Player.instance.getWeaponByType(itemId));
				delete GD_Player.instance.addNewWeapons[itemId];
			}
		}
		
		
		/**
		 *奖励飞行结束 
		 * 
		 */		
		private function getOver():void
		{
			this.rewardData.gainReward();
		}
		
		/**
		 *释放 
		 * 
		 */		
		override public function release():void
		{
			super.release();
			this.removeListener();
			if(this.icon)
			{
				this.icon.release();
				this._icon=null;
			}
			this._velocity=null;
		}

		/*getter & setter*/

		public function get limit_y():Number
		{
			return _limit_y;
		}

		public function set limit_y(value:Number):void
		{
			_limit_y = value;
		}

		public function get velocity():GU_Vector2D
		{
			return _velocity;
		}

		public function set velocity(value:GU_Vector2D):void
		{
			_velocity = value;
		}

		public function get isDropping():Boolean
		{
			return _isDropping;
		}

		public function set isDropping(value:Boolean):void
		{
			_isDropping = value;
		}
		
		public function get rewardData():GD_Reward
		{
			return this.gameData as GD_Reward;
		}

		public function get icon():ViewIcon
		{
			return _icon;
		}

		public function get getPoint():Point
		{
			return _getPoint;
		}

		public function get isGetting():Boolean
		{
			return _isGetting;
		}

		public function set isGetting(value:Boolean):void
		{
			_isGetting = value;
		}

		public function get jumpNum():int
		{
			return _jumpNum;
		}

		public function set jumpNum(value:int):void
		{
			_jumpNum = value;
		}

		public function get initVY():Number
		{
			return _initVY;
		}

		public function set initVY(value:Number):void
		{
			_initVY = value;
		}

//		public function get pointObj():Object
//		{
//			return _pointObj;
//		}

		/**
		 * @private
		 */
//		public function set pointObj(value:Object):void
//		{
//			_pointObj = value;
//		}


	}
}